﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.SS.Formula.PTG;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using Warehouse.API.Applcation.Command.InjureCommand;
using Warehouse.Domain.DTO;

namespace Warehouse.API.Controllers
{
    /// <summary>
    /// 报损管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
   
    public class InjureController : ControllerBase
    {

        IMediator mediator;

        public InjureController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 添加报损管理
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddInjure(AddInjureDto command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                command.user_id = Convert.ToInt32(uid);
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.office_id = sid;
                command.injureDetails.FirstOrDefault().office_id = sid;
                command.injureDetails.FirstOrDefault().user_id = Convert.ToInt32(uid);
                result.Data = await mediator.Send(new InjureAddCommand { addDto = command });
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }

       

        /// <summary>
        /// 删除报损管理
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> DelInjure(InjureDelCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }


        /// <summary>
        /// 修改报损管理
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdInjure(AddInjureDto command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                command.user_id = Convert.ToInt32(uid);
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.office_id = sid;
                command.injureDetails.FirstOrDefault().office_id = sid;
                command.injureDetails.FirstOrDefault().user_id = Convert.ToInt32(uid);
                result.Data = await mediator.Send(new InjureUpdCommand{ addDto = command});
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 修改报损审核状态
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdStateInjure(InjureAuditCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                command.user_id = Convert.ToInt32(uid);
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.office_id = sid;
                command.office_id = sid;
                command.user_id = Convert.ToInt32(uid);
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }




    }
}
